package br.ufrj.xml.model.persistence.dao.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import br.ufrj.xml.model.persistence.dao.RelationshipDAO;
import br.ufrj.xml.model.persistence.entity.RelationshipVO;
import br.ufrj.xml.model.persistence.entity.SchemaVO;
import br.ufrj.xml.model.persistence.entity.TableVO;

public class RelationshipJdbcDAO extends GenericJdbcDAO<RelationshipVO> implements RelationshipDAO {

	public RelationshipJdbcDAO(Connection connection) {
		super(connection);
	}

	public List<RelationshipVO> findChildren(String nmTabela, String nmEsquema) {
		
		String sql = "SELECT " + RelationshipVO.COLUNA_RELACIONAMENTO_PAI + ", " + RelationshipVO.COLUNA_RELACIONAMENTO_FILHO + 
					 " FROM "  + nmEsquema + "." + RelationshipVO.TABELA_RELACIONAMENTO_AUX.replace(SchemaVO.PARAM_NM_SCHEMA , nmEsquema) + 
					 " WHERE " + RelationshipVO.COLUNA_RELACIONAMENTO_PAI + " = ?"; 

		List<RelationshipVO> lista = query(sql,
											new Object[]{ nmTabela.toUpperCase() },
											new int[]{ Types.VARCHAR });
		return lista;
	}

	public List<RelationshipVO> findParent(String nmTabela, String nmEsquema) {
		String sql = "SELECT " + RelationshipVO.COLUNA_RELACIONAMENTO_PAI + ", " + RelationshipVO.COLUNA_RELACIONAMENTO_FILHO + 
		 " FROM "  + nmEsquema + "." + RelationshipVO.TABELA_RELACIONAMENTO_AUX.replace(SchemaVO.PARAM_NM_SCHEMA , nmEsquema) + 
		 " WHERE " + RelationshipVO.COLUNA_RELACIONAMENTO_FILHO + " = ?"; 

		List<RelationshipVO> lista = query(sql,
										new Object[]{ nmTabela.toUpperCase() },
										new int[]{ Types.VARCHAR });
		return lista;
	}

	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void create(RelationshipVO entity) {
		// TODO Auto-generated method stub
		
	}

	public void delete(Integer id) {
		// TODO Auto-generated method stub
		
	}

	public List<RelationshipVO> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<RelationshipVO> findPageByPage(int firstResult, int maxResults) {
		// TODO Auto-generated method stub
		return null;
	}

	public RelationshipVO read(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public void update(RelationshipVO entity) {
		// TODO Auto-generated method stub
		
	}

	public RelationshipVO mapRow(ResultSet result) throws SQLException {
		int i = 1;
		RelationshipVO relationshipVO = new RelationshipVO();
		relationshipVO.setTableParent(new TableVO(result.getString(i++)));
		relationshipVO.setTableChild(new TableVO(result.getString(i++)));
		return relationshipVO;
	}

}
